<?php 
namespace app\superadmin\controller;
use think\Db;
use think\Controller;
class Occupation extends Base{
	public function index(){
		$occs=DB::name('occupation')->paginate(10);
		$this->assign('occs',$occs);

    //序号
    $page=request()->param('page');
    $page||($page=1);
    $this->assign('page',$page);
		return $this->fetch();
	}

	  //屏蔽/解除屏蔽
	public function shield($oid){

  $ustatenum=Db::name("occupation")->where("oid='$oid'")->value('ostate');

  if($ustatenum==0){
    if (Db::name("occupation")->where("oid='$oid'")->update(['ostate'=>1])) {
      $this->redirect('index');
    }else{
      $this->error('解除屏蔽失败');
    }

  }
  else{
    if (Db::name("occupation")->where("oid='$oid'")->update(['ostate'=>0])) {
      $this->redirect('index');
    }else{
      $this->error('屏蔽失败');
    }    
  }


	}
//添加职业方向
	public function addoccus(){
		$data=input("post.");
		// dump($data);
		$file=request()->file("opic");
    // dump($file);
    $picname="";
    if ($file) {
      # code...
      $info=$file->rule('uniqid')->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'static'.DS.'occupic');
      if ($info) {
        # code...
        $picname=$info->getFilename();

      }else{
        $this->error($file->getErrot());
      }
    }
    $data+=['opic'=>$picname,
            
            ];
            // dump($data);
    $res=Db::name('occupation')->insert($data);
    if ($res) {
      # code...
      $this->redirect('index');
    }else{
      $this->error('职业方向添加失败！');
    }
	}
	public function change($oid){
		$datas=Db::name('occupation')->where('oid',$oid)->select();
		$this->assign('datas',$datas);
		return $this->fetch();
	}


	//修改职业方向信息
  public function dochange($oid){
    // dump($sid);
    $data=input("post.");
    // dump($data);

    // if ($spic!=0) {
    //   $row=Db::name('star_xy')->where('sid',$sid)->select();
    //   unlink("../public/static/upload".$row["spic"]);
    // }
    $file = request()->file('opic');
     // dump($files);
    // dump($file);
    $picname="";
    if ($file==null) {
      # code...
       $row=Db::name('occupation')->where('oid',$oid)->select();
      $file=$row[0]['opic'];
      $picname=$row[0]['opic'];
       // dump($row[0]['spic']);
    }else{
     
       // if($file){
       
          # code...
          $info=$file->rule('uniqid')->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'static'.DS.'occupic');
          if ($info) {
          # code...
          $picname=$info->getFilename();
        }else{
          // echo $f->getError();
          $this->error($file->getError());
        }
        
        $row1=Db::name('occupation')->where('oid',$oid)->select();
        unlink('../public/static/occupic/'.$row1[0]['opic']);

     
      
    }
     $data+=['opic'=>$picname];
    $up=Db::name('occupation')->where('oid',$oid)->update($data);
    if ($up) {
      # code...
      $this->redirect('index');
    }else{
      $this->error('请至少修改一条信息');
    }
  }

  //添加FAQ
  public function addfaq($oid){
    $faqs=Db::name('faq')->where('oid',$oid)->paginate(5);
    $this->assign('faqs',$faqs);
     //序号
$occs=Db::name('occupation')->where('oid',$oid)->select();
$this->assign('occs',$occs);
    $page=request()->param('page');
    $page||($page=1);
    $this->assign('page',$page);
    // $this->assign('oid',$oid);
    return $this->fetch();
  }

  //插入FAQ数据
  public function doaddfaqs(){
    $datas=input("post.");
    // dump($datas);
    $res=Db::name('faq')->insert($datas);
    if ($res) {
      # code...
      $this->redirect("addfaq",["oid"=>$datas['oid']]);
    }else{
      $this->error('添加失败！');
    }
  }

    //屏蔽
  public function shield2($fid,$oid){
    $res=Db::name('faq')->where('fid',$fid)->update(['fstatus'=>1]);
    if ($res) {
      # code...
      $this->redirect('addfaq',['oid'=>$oid]);
    }else{
      $this->error('封禁失败！');
    }
  }
  //解封
  public function shield3($fid,$oid){
    $res=Db::name('faq')->where('fid',$fid)->update(['fstatus'=>0]);
    if ($res) {
      # code...
      $this->redirect('addfaq',['oid'=>$oid]);
    }else{
      $this->error('解除封禁失败！');
    }
  } 
}